package class06;

import java.util.HashMap;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class CopyListWithRandom {

    public static class Node{
        int val;
        Node next;
        Node random;

        public Node(int val){
            this.val=val;
            this.next=null;
            this.random = null;
        }
    }

    public static Node copyRandomList1(Node head){
        HashMap<Node,Node> map = new HashMap<>();
        Node cur = head;
        while (cur!=null){
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while (cur!=null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }

    public static Node copyRandomList2(Node head){
        if (head==null)
            return null;
        Node cur = head;
        Node next = null;
        while (cur!=null){
            next = cur.next;
            cur.next = new Node(cur.val);
            cur.next.next = next;
            cur = next;
        }
        cur = head;
        Node copy = null;
        while (cur!=null){
            next = cur.next.next;
            copy = cur.next;
            copy.random = copy.random!=null?copy.random.next:null;
            cur = next;
        }
        Node res = head.next;
        cur=head;

        while (cur!=null){
            next = cur.next.next;
            copy = cur.next;
            cur.next = next;
            copy.next = next!=null?next.next:null;
            cur = next;
        }
        return res;
    }
}
